package com.java.web.manageweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.java.base.pojo.EasyUIResult;
import com.java.manage.pojo.TbItem;
import com.java.manage.service.TbItemDescService;
import com.java.manage.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName ItemController
 * @Description 后台添加菜单
 * @Author Trigger Wu
 * @Date 2018/9/716:09
 * @Version 1.0
 **/

@Controller
@RequestMapping("/rest/item")
public class ItemController {
    @Autowired
    private TbItemService tbItemService;
    @Autowired
    private TbItemDescService itemDescService;
    /*
     *   查询类型列表
     *
     * @author Tigger Wu
     * @date 2018/9/10 18:05
     * @param [item, desc]
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveItem(TbItem item , @RequestParam(value = "desc")String desc){
        try {
            if(StringUtils.isEmpty(item.getTitle())){
            //参数错误
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
            Boolean result = tbItemService.saveItem(item,desc);
            if(!result){
                //保存失败
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
/*
 *  查询列表
 * 
 * @author Tigger Wu
 * @date 2018/9/10 15:32
 * @param []
 * @return org.springframework.http.ResponseEntity<com.java.base.pojo.EasyUIResult>
 */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult>queryList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "30") Integer rows){
        try {
            Page<TbItem> itemPage = new Page<TbItem>(page,rows);
            EntityWrapper<TbItem> wrapper = new EntityWrapper<TbItem>();
            wrapper.orderBy("created",false);
            Page<TbItem> itemPage1 = tbItemService.selectPage(itemPage,wrapper);
            EasyUIResult ez = new EasyUIResult();
            ez.setTotal(itemPage1.getTotal());
            ez.setRows(itemPage1.getRecords());
            return  ResponseEntity.ok(ez);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateItem(TbItem item, @RequestParam("desc") String desc,
                                           @RequestParam("itemParams") String itemParams) {
        try {

            if (StringUtils.isEmpty(item.getTitle())) {
                // 参数有误, 400
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }

            // 编辑商品
            this.tbItemService.updateItem(item,desc,itemParams);
            // 204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
           e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
